﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AspZone.Commerce.Service;
using AspZone.Commerce.Web.Models;

namespace AspZone.Commerce.Web.Controllers
{
    public class CatalogController : Controller
    {
        private ICatalogService catalogService;

        public CatalogController(ICatalogService catalogService)
        {
            this.catalogService = catalogService;
        }

        public ActionResult Category(string seoName)
        {
            var category = this.catalogService.GetCategoryBySEOName(seoName);
            //var products = this.catalogService.GetProductsByCategory(seoName);

            var products = from p in this.catalogService.GetProductsByCategory(seoName)
                           select new ProductSummary
                           {
                               Id = p.Id,
                               Name = p.Name,
                               SEOName = p.SEOName,
                               ListPrice = p.Price,
                               DiscountPercent = p.DiscountPercent,
                               ThumbnailPath = p.Images.Count > 0 ? p.Images[0].ThumbnailPath : null
                           };


            var viewData = new CatalogCategoryViewData { Category = category, Products = products.ToList() };
            
            return View(viewData);
        }

        public ActionResult Product(string seoName)
        {
            var data = this.catalogService.GetProductBySEOName(seoName);
            ViewData.Add("Product", data);
            return View();
        }
    }
}
